package com.lunatic.rocketmq.demo;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.Random;

public class TagFilterProducer {
    public static void main(String[] args) throws Exception {
        // 1. 初始化生产者
        DefaultMQProducer producer = new DefaultMQProducer(
                "producer_group1");
        producer.setNamesrvAddr("192.168.101.106:9876");
        producer.start();

        String[] s = new String[] {"TagF","TagA","TagB", "TagC"};
        // 2. 发送消息（使用 Java 17 特性：var 关键字）
        for (int i = 0; i < 100; i++) {
            var msg = new Message(
                    "TestFilterTopic",
                    s[new Random().nextInt(0, 4)],
                    ("Hello RocketMQ " + i).getBytes());
            // 延时级别 1s 5s 10s 30s 1m 2m 。。。
//            msg.setDelayTimeLevel(3);
            // 10s后发送
//            msg.setDelayTimeSec(10);
            // 指定时间发送
            msg.setDelayTimeMs(25920000);
            // 同步发送，需要等待broker确认
            SendResult result = producer.send(msg);
            System.out.printf("Sent message: %s%n", result);
        }


        System.out.println("over");
        System.in.read();

        // 3. 关闭生产者
        producer.shutdown();
    }
}